﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Soussous.Business;
using Soussous.Tools;
using Soussous.DAO;
using Soussous.DAO.Impl;
using System.Collections.Generic;

namespace Soussous.Template
{
    public partial class subscribe : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Submit_form(object sender, EventArgs e)
        {
            List<TextBox> listingBox = new List<TextBox>();
            
            listingBox.Add(lastName);
            listingBox.Add(firstName);
            listingBox.Add(email);
            listingBox.Add(password);
            listingBox.Add(cpassword);

            string [] reference = {"lastname","firstname","email","password","confirm password"}; 

            string msg = "";
            int i = 0;
            
            foreach (TextBox field in listingBox) 
            {
                if (field.Text == "") 
                {
                    msg += "<li>Please fill the "+reference[i]+"</li>\n";
                }
                ++i;
            }

            if (email.Text != "")
            {
                if (!Helper.checkMail(email.Text))
                {
                    msg += "<li>The email address filled seems invalid  </li>";

                }
                else if (UserDAO.getInstance().searchMailUser(email.Text))
                {
                    msg += "<li>The email address filled seems already exist </li>";

                }
            }
            if (!password.Text.Equals(cpassword.Text))
            {
                msg += "<li>The passwords filled are not same</li>";
                
            }

            error_msg.InnerHtml = msg;

            if (msg == "")
            {
                User theUser = new User();
                theUser.Firstname = firstName.Text;
                theUser.Lastname = lastName.Text;
                theUser.Email = email.Text;
                theUser.Password = password.Text;

                UserDAO.getInstance().insert(theUser);
                HttpContext.Current.Session["user"] = theUser;

                string exp = "slowtun@gmail.com";
                string to = email.Text;
                string sub = "Confirm Inscription";
                string body = "Dear member,\n\n";
                body += "In the name of all team Sousous, we would like to welcome you on our site.\n";
                body += "We hop that you will spend of good time on our site.\n\n";
                body += "Soussous team";
                Helper.SendMail(exp, to, sub, body);

                Response.Redirect("home.aspx");
            
            }   
        }
    }
}
